import hx.events.Event;
import motion.Actuate;
import hx.events.Keyboard;
import hx.events.KeyboardEvent;
import spine.attachments.Attachment;
import hx.display.Quad;
import hx.display.Spine;
import hx.display.SpineSprite;
import hx.display.FPS;
import hx.display.Image;
import hx.assets.Assets;
import hx.display.Stage;

class GameStage extends Stage {
	override function onStageInit() {
		super.onStageInit();
		// 代码开始的地方...
		// var spine = new SpineSprite();
		var assets = new Assets();
		assets.loadSpineAtlas("assets/sxk.png", "assets/sxk.atlas");
		assets.loadString("assets/sxk.json");
		assets.loadSpineAtlas("assets/durianPig.png", "assets/durianPig.atlas");
		assets.loadString("assets/durianPig.json");
		assets.loadSpineAtlas("assets/weapon.png", "assets/weapon.atlas");
		assets.loadString("assets/weapon.json");

		assets.loadSpineAtlas("assets/beanTurtle.png", "assets/beanTurtle.atlas");
		assets.loadString("assets/beanTurtle.json");

		assets.loadSpineAtlas("assets/helmet.png", "assets/helmet.atlas");
		assets.loadString("assets/helmet.json");

		assets.loadSpineAtlas("assets/clothes.png", "assets/clothes.atlas");
		assets.loadString("assets/clothes.json");

		assets.onComplete((a) -> {
			var spine = new SpineSprite(assets.getSkeletonData("durianPig"));
			this.addChild(spine);
			spine.x = stage.stageWidth / 2;
			spine.y = 800;
			spine.play("run");
			spine.scaleX = spine.scaleY = 0.5;

			var role = new SpineSprite(assets.getSkeletonData("sxk"));
			this.addChild(role);
			role.play("attack");

			// 武器绑定
			var weapon = new Spine(assets.getSkeletonData("weapon"));
			weapon.skeleton.data.findSlot("weapon").attachmentName = "weapon_07";
			weapon.skeleton.setSlotsToSetupPose();
			role.skeleton.findSlot("weapon").attachment = null;
			role.bindSlot("weapon", weapon);
			spine.bindSlot("follow", role);

			// 衣服绑定
			var clothes = new Spine(assets.getSkeletonData("clothes"));
			clothes.skeleton.data.findSlot("clothes").attachmentName = "clothes_01";
			clothes.skeleton.setSlotsToSetupPose();
			role.skeleton.findSlot("clothes").attachment = null;
			role.bindSlot("clothes", clothes, (clothes) -> {
				clothes.rotation += 180;
				clothes.scaleX *= -1;
			});

			// 头盔绑定
			var helmet = new Spine(assets.getSkeletonData("helmet"));
			helmet.skeleton.data.findSlot("helmet").attachmentName = "helmet_03";
			helmet.skeleton.setSlotsToSetupPose();
			// role.skeleton.findSlot("helmet").attachment = null;
			role.bindSlot("helmet", helmet, (helmet) -> {
				helmet.y += 13;
			});

			spine.bindSlot("follow", role);

			var index = 1;

			this.stage.addEventListener(KeyboardEvent.KEY_DOWN, (e:KeyboardEvent) -> {
				switch e.keyCode {
					case Keyboard.A:
						index++;
						weapon.skeleton.data.findSlot("weapon").attachmentName = "weapon_0" + Std.random(7);
						weapon.skeleton.setSlotsToSetupPose();

						clothes.skeleton.data.findSlot("clothes").attachmentName = "clothes_0" + Std.random(4);
						clothes.skeleton.setSlotsToSetupPose();

						helmet.skeleton.data.findSlot("helmet").attachmentName = "helmet_0" + Std.random(4);
						helmet.skeleton.setSlotsToSetupPose();
					case Keyboard.D:
						index--;
						weapon.skeleton.data.findSlot("weapon").attachmentName = "weapon_0" + index;
						weapon.skeleton.setSlotsToSetupPose();

						clothes.skeleton.data.findSlot("clothes").attachmentName = "clothes_0" + index;
						clothes.skeleton.setSlotsToSetupPose();

						helmet.skeleton.data.findSlot("helmet").attachmentName = "helmet_0" + index;
						helmet.skeleton.setSlotsToSetupPose();
				}
			});

			// for (i in 0...30) {
			// 	var spine = new SpineSprite(assets.getSkeletonData("beanTurtle"));
			// 	this.addChild(spine);
			// 	spine.x = Math.random() * stage.stageWidth;
			// 	spine.y = Math.random() * stage.stageHeight;
			// 	spine.play("run");
			// 	spine.scaleX = spine.scaleY = 0.35;

			// 	var role = new SpineSprite(assets.getSkeletonData("sxk"));
			// 	this.addChild(role);

			// 	role.play("idle");
			// 	var weapon = new Spine(assets.getSkeletonData("weapon"));
			// 	weapon.skeleton.data.findSlot("Right_Hand_Weapon_F").attachmentName = "Weapon_08";
			// 	weapon.skeleton.setSlotsToSetupPose();
			// 	role.bindSlot("Right_Hand_Weapon_F", weapon);
			// 	spine.bindSlot("follow", role);
			// }
		});
		assets.start();

		var fps = new FPS();
		this.topView.addChild(fps);
	}
}
